<template>
	<view class="content" :style="'background:#f7f7f7;'">
		<image class="poster-image" :src="poster_image" mode=""></image>
		<view class="bottom-btn" :style="'background:#FFFFFF;'">
			<view class="btn" :style="'background:#E9564F;'" @click="downLoadPoster">
				<text :style="'color:#FFFFFF'">保存分享图</text>
			</view>
		</view>
	</view>
</template>

<script>
	var util = require('@/js/util.js')
	export default {
		data() {
			return {
				poster_image:'',
				luttery_id:''
			};
		},
		onShareAppMessage() { // 分享 buttom share
			return {
				title: '抽奖助手',
				path: '/pages/index/index?parent_id='+uni.getStorageSync('member_id')+'&prizedraw_id='+this.luttery_id
			}
		},
		onLoad(e) {
			this.luttery_id = e.id
			this.getUserList()
		},
		methods:{
			downLoadPoster(){
				let _this = this
				uni.getSetting({
					success(res) {
						console.log(res.authSetting)
						if(!res.authSetting['scope.writePhotosAlbum']){
							console.log("bbbb")
							uni.authorize({
								scope:"scope.writePhotosAlbum",
								success: (res) => {
									console.log("ddd",res)
								},
								fail: (e) => {
									console.log("CCC",e)
									uni.showModal({
										title:"提示",
										content:"保存海报需要您授权保存！",
										success: (res) => {
											if(res.confirm){
												uni.openSetting({
													success(res) {
														console.log(res.authSetting)
														if(res.authSetting["scope.writePhotosAlbum"]){
															_this.downloadfliea()
														}
													},
													fail: (e) => {
														console.log("EEE",e)
													}
												});
											}
										}
									})
								}
							})
						}else{
							console.log("aaaa")
							_this.downloadfliea()
						}
					}
				})
			},
			downloadfliea(){
				uni.showLoading({
					title:'保存中...'
				})
				console.log("1111",this.poster_image)
				uni.downloadFile({
					url:this.poster_image,
					success(res) {
						console.log(res)
						uni.saveImageToPhotosAlbum({
							filePath:res.tempFilePath,
							success:(res)=>{
								console.log(res)
								uni.hideLoading()
								uni.showToast({
									title:'保存成功！',
									icon:'success'
								})
							},
							fail:(e)=>{
								console.log(e)
							}
						})
					},
					fail(e) {
						console.log(e)
					}
				})
			},
			getUserList(){
				let _self = this
				uni.request({
					url: util.getUrl("prizedraw/poster"),
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation':uni.getStorageSync("token")
					},
					data:{
						prizedraw_id:_self.luttery_id,
					},
					method: 'POST',
					success: res => {
						if(res.data.code == 1){
							_self.poster_image = res.data.data.poster
						}else{
							if(res.data.code == 500){
								uni.navigateTo({
									url:'../../../pages/login/login'
								})
							}
						}
					},
				});
			},
		}
	}
</script>

<style lang="scss">
	.bottom-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 750upx;
		height: 140upx;
		position: fixed;
		bottom: 0;
		left: 0;
		border-radius: 20upx 20upx 0px 0px;
		.btn{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 690upx;
			height: 80upx;
			font-size: 34upx;
			border-radius: 10upx;
		}
	}
	.poster-image{
		width: 690upx;
		height: 1227upx;
		margin-bottom: 31upx;
		background: #FFFFFF;
		border-radius: 10upx;
	}
	.content{
		display: flex;
		align-items: center;
		flex-direction: column;
		overflow: hidden;
		position: relative;
		width: 750upx;
		min-height: 100vh;
		box-sizing: border-box;
		padding: 30upx 30upx 140upx 30upx;
		font-family: PingFang SC;
	}
</style>
